﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace WebForm
{
    public partial class ThanhToan : System.Web.UI.Page
    {
        private Library.BIZ.HoaDonXuatBIZ hoadonxuat;
        private List<Library.BIZ.ChiTietHoaDonXuatBIZ> listchitiet;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["cart"] == null)
            {
                Response.Redirect("Default.aspx");
            }
            
        }
        protected void Button_QuayLai_Click(Object sender, EventArgs e)
        {
            Response.Redirect("GioHang.aspx");
        }
        protected void Button_XacNhan_Click(Object sender, EventArgs e)
        {
            if (textbox_hoten.Text.Trim() == "" || textbox_dienthoai.Text.Trim() == "" || textbox_diachi.Text.Trim() == "")
            {
                label_error.Text = "Phải nhập đầy đủ thông tin";
                return;
            }
            label_error.Text = "";

            hoadonxuat = new Library.BIZ.HoaDonXuatBIZ();
            hoadonxuat.Ngay = DateTime.Now;

            long tong = 0;
            
            DataTable table = (DataTable)Session["cart"];

            foreach (DataRow r in table.Rows)
            {
                tong += Int32.Parse(r["thanhtien"].ToString());
            }

            hoadonxuat.GiaTri = tong;
            hoadonxuat.TenKH = textbox_hoten.Text.Trim();
            hoadonxuat.SoDienThoaiKH = textbox_dienthoai.Text.Trim();
            hoadonxuat.DiaChiKH = textbox_diachi.Text.Trim();

            int id_hoadonxuat = hoadonxuat.Add();

            foreach (DataRow r in table.Rows)
            {
                Library.BIZ.ChiTietHoaDonXuatBIZ chitiet = new Library.BIZ.ChiTietHoaDonXuatBIZ();
                chitiet.ID_HoaDonXuat = id_hoadonxuat;
                chitiet.ID_SanPham = Int32.Parse(r["id"].ToString());
                chitiet.DonGia = long.Parse(r["dongia"].ToString());
                chitiet.SoLuong = Int32.Parse(r["soluong"].ToString());

                chitiet.Add();
            }

            Session.Contents.RemoveAll();
            Response.Redirect("Thongbao.aspx");

        }
    }
}